
package editor.map.minimap
{
	import editor.Notification.V2MNotification;
	import editor.common.AppFacade;
	import editor.common.Common;
	import editor.map.TileLayer;
	import editor.map.elements.Npc;
	import editor.map.elements.Teleport;
	import editor.map.elements.Tile;
	
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	
	/**
	 * @author 缺硒
	 */
	public class Minimap extends Sprite
	{
		public static const TRUE_COLOR:uint = 0xffffffff;
		public static const FALSE_COLOR:uint = 0xff000000;
		public static const MINI_NPC_COLOR:uint = 0x0088ff;
		public static const MINI_OUT_COLOR:uint = 0x22ff22;
		public static const MINI_IN_COLOR:uint 	= 0xff0000;
		
		private var _cdTime:int = 16;
		private var _timer:Timer;
		private var _mapBmd:BitmapData;
		private var _bmpRect:Rectangle = new Rectangle(0,0,0,0);
		
		// 构造函数
		public function Minimap()
		{
			_timer = new Timer(_cdTime);
			_timer.addEventListener(TimerEvent.TIMER, onDraw);
			_timer.start();
			_bmpRect.width = TileLayer.MAP_LOGIC_WIDTH;
			_bmpRect.height = TileLayer.MAP_LOGIC_HEIGHT;
			addEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function onClick(evt:MouseEvent):void
		{
			AppFacade.getIntance().sendNotification(V2MNotification.SET_MAP_VIEW,
				{
					vx:evt.localX * Tile.TILE_WIDTH,
					vy:evt.localY * Tile.TILE_WIDTH
				});
		}
		
		/**
		 * 绘制小地图
		 * @param evt 
		 */		
		private function onDraw(evt:TimerEvent):void
		{
			graphics.clear();
			_mapBmd = new BitmapData(TileLayer.MAP_LOGIC_WIDTH, TileLayer.MAP_LOGIC_HEIGHT, true, 0x000000);
			if(TileLayer.mapVector != null) _mapBmd.setVector(_bmpRect, TileLayer.mapVector);
			graphics.beginBitmapFill(_mapBmd, null, false);
			graphics.drawRect(0,0,_bmpRect.width,_bmpRect.height);
			graphics.endFill();
			drawMapElement();
			drawViewBox();	
		}
		
		//绘制npc和传送门点
		private function drawMapElement():void
		{
			//绘制npc
			var npcList:Vector.<Npc> = Common.map.npcLayer.npcList;
			for each(var npc:Npc in npcList)
			{
				graphics.beginFill(MINI_NPC_COLOR);
				graphics.drawCircle(npc.x/Tile.TILE_WIDTH, npc.y/Tile.TILE_WIDTH, 5);
				graphics.endFill();
			}
			
			//绘制出入口
			var inPort:Teleport = Common.map.npcLayer.inGate;
			if(inPort != null)
			{
				graphics.beginFill(MINI_IN_COLOR);
				graphics.drawCircle(inPort.x / Tile.TILE_WIDTH, inPort.y / Tile.TILE_WIDTH,8);
				graphics.endFill();
			}
			
			var outPort:Teleport = Common.map.npcLayer.outGate;
			if(outPort != null)
			{
				graphics.beginFill(MINI_OUT_COLOR);
				graphics.drawCircle(outPort.x / Tile.TILE_WIDTH, outPort.y / Tile.TILE_WIDTH,8);
				graphics.endFill();
			}
		}
		
		//绘制可视范围框
		private function drawViewBox():void
		{
			this.graphics.lineStyle(1, 0xccaa66);
			
			var vx:int = (- Common.map.gmap.x) / Tile.TILE_WIDTH;
			var vy:int = (- Common.map.gmap.y) / Tile.TILE_WIDTH;
			
			this.graphics.drawRect(vx, vy, stage.nativeWindow.width / Tile.TILE_WIDTH, stage.nativeWindow.height / Tile.TILE_WIDTH);
			this.graphics.endFill();
		}
		//
	}
}